package me.leon.ext.android

import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import kotlin.math.roundToInt

/**
 * @author : Leon Shih
 * @time : 2019/12/16 0016 16:21
 * @e-mail : deadogone@gmail.com
 * @desc : context extension
 */

/** convert dp to pixel */
fun Context.dp2px(dp: Int): Int = (resources.displayMetrics.density * dp).roundToInt()

/** convert colorRes to Color Int */
fun Context.colorRes(@ColorRes colorRes: Int): Int = ContextCompat.getColor(this, colorRes)

/** convert colorRes to Color Int */
fun Context.drawableRes(@DrawableRes drawableRes: Int): Drawable? =
    ContextCompat.getDrawable(this, drawableRes)

/** toast what you want w/o appName */
fun Context.show(text: String?) =
    Toast.makeText(this.applicationContext, text, Toast.LENGTH_SHORT).apply { setText(text) }.show()

/** toast what you want w/o appName */
fun Context.show(@StringRes text: Int) =
    Toast.makeText(this.applicationContext, text, Toast.LENGTH_SHORT).apply { setText(text) }.show()
